require "Common/define"
require "Logic/Config"

UIBigMap = {};
HelperFunc.SetUIEnv(UIBigMap)
local this = UIBigMap;

this.LuaBehaviour = nil;
this.transform = nil;
this.gameObject = nil;
this.hasInit = false;
this.mapSize = {};

this.btnOpenUnLockPage = nil;
this.dotNode = {};
this.drawWrappers = {};
this.ActiveBanch = {};
this.FriendBanch = {};
this.showBar = nil;
this.textNowChapinfo = nil;
this.textNowSteep = nil;
this.mapName = nil;
this.FriendContent = nil
this.chapterContent = nil
this.mapContent = nil;
this.mapList = nil;
this.btn_MapDot = nil;
this.btn_chapterDot = nil;
this.mapObjTab = {}
this.LineObjTab = {}
this.lineTab = {}
this.NowPointTab = {}
this.chapterBg = nil
this.friendBg = nil
this.btn_closeChap = nil
this.CountryName = nil
this.FriendSTab = {}
this.FriendList = nil;
this.dynamicList = nil;
this.BtnFriback = nil;
this.ActivePanel = nil;
this.dynamicListActivity = nil;
this.ActiveItem = nil;
this.btnSpeed = nil;
this.BtnBackActive = nil;
this.closeBar = nil;
this.btnAgree = nil;
this.SpeedPanelAgreeText = nil;
this.BtnBackSpeedPanel = nil;
this.backToSpeed = nil;
this.btnActive = nil;
this.btnVoice = nil;
this.TabHighTeamIcon = {}
this.ModelObj = {}
this.GotBtn = nil;
this.Backbtn = nil;

this.isZoom = false;
this.OneDis = nil;
this.TwoDis = nil;

this.pageController = nil;
this.Roles = {};
this.drawWrapper = nil;
--功能解锁页面
this.unLockPage = nil;
this.unLockList = nil;
this.unLockDatas = {}
this.btnCloseUnLockPage = nil;
this.unLockPageController = nil;
this.dropMapPageController = nil;
this.chapterItems = {};
this.chapterData = {}
this.mapDropPage = nil;
this.dropListData = {}

local MAP_SCALE = 1.2;
function UIBigMap.Init( data, _type )
    this.init_data = data
    this.open_type = _type
    local name, packageName = getUIName(PanelNames.UIBigMap);
    panelMgr:ShowUI(name, packageName, this.OnCreate, Config.useLocalUI);
    --[[local tableHeros = logicMgr.HeroManager.getOwnerHeroList(true);
    local formation={}
    for i = 1, 1 do
        table.insert(formation,tableHeros[i].partnerId);
    end
    --dungeonHandler.sendBossMessage(428,formation);]]
end

function UIBigMap.OnCreate(obj)
    gameObject = obj;

    if not hasInit then
        hasInit = true;
        btnOpenUnLockPage = obj:GetChild("Btn_UnlockFunc");
        --textNowChapinfo = obj:GetChild("textNowChapinfo");
        textOverPlayer = obj:GetChild("Text_OverPlayer");
        textNowSteep = obj:GetChild("textNowSteep");
        --pageController = obj:GetController("c1")
        --mapName = obj:GetChild("mapName");
        --showBar = obj:GetChild("showBar");
        mapList = obj:GetChild("mapList");
        --FriendContent = {};--3个好友位
        --for i = 1, 3 do
        --    local item = showBar:GetChild("friend" .. i);
        --    table.insert(FriendContent, item);
        --end
        --chapterContent = showBar:GetChild("chapterContent");
        mapList.numItems = 1;
        mapContent = mapList:GetChildAt(0);
        --local name, package = getUIName(PanelNames.MapDot);
        --btn_MapDot = newGComponent(name, package);
        --btn_MapDot = obj:GetChild("btn_MapDot"):GetComponent("Button");
        --btn_chapterDot=obj:GetChild("btn_chapterDot"):GetComponent("Button"); 关卡进度item
        
        unLockPageController = obj:GetController("unLockPage");
        unLockPage = obj:GetChild("unLockPage");
        this.Btn_ClickArea = obj:GetChild("Btn_ClickArea");
        unLockList = unLockPage:GetChild("List_UnlockTips")
        btnCloseUnLockPage = unLockPage:GetChild("Btn_BgClose");
        mapDropPage = obj:GetChild("mapDropPage");
        btnCloseMapDrop = mapDropPage:GetChild("Btn_Bgclose");
        btnbackMapDrop=mapDropPage:GetChild("btnBack");
        dropMapPageController = obj:GetController("dropMapPage");
        chapterItems = {}
        for i = 1, 13 do
            local item = mapContent:GetChild("Btn_Chapter" .. i)
            table.insert(chapterItems, item)
        end
        --this.detailController = obj:GetController("detail");
        --this.detail = obj:GetChild("detail");
        --this.detailBgClose = this.detail:GetChild("bgClose");
        --this.detailTitle = this.detail:GetChild("title");
        --this.detailText = this.detail:GetChild("text");
        --this.detailText.text = getLanguage("text_BigMap1")
        --this.detailText2 = this.detail:GetChild("text2");
        --this.detailText2.text = getLanguage("text_BigMap2")
        --this.detailRewardList = this.detail:GetChild("listReward");
        --this.detailBtnGo = this.detail:GetChild("btnGo");
        --this.detailBtnReward = this.detail:GetChild("btnReward");
        --this.detailPoint = this.detail:GetChild("point");
        --this.detailStateController = this.detail:GetController("state");
        --this.detailRewardController = this.detail:GetController("reward");
        --chapterBg = obj:GetChild("chapterBg").transform;
        --friendBg = showBar:GetChild("friendBg");
        --closeBar = obj:GetChild("btnShowBarClose");
        ----btn_closeChap = obj:GetChild("friendBg/btn_closeChap").transform;
        ----CountryName = obj:GetChild("CountryName"):GetComponent("TextMeshProUGUI");
        --FriendList = obj:GetChild("FriendList");
        --dynamicList = obj:GetChild("dynamicList");
        --BtnFriback = obj:GetChild("BtnFriback");
        ----ActiveItem = obj:GetChild("ActiveItem");
        --ActivePanel = obj:GetChild("ActiveList");
        --dynamicListActivity = obj:GetChild("dynamicListActivity");
        --btnSpeed = obj:GetChild("btnSpeed");
        --BtnBackSpeedPanel = obj:GetChild("btnBackSpeed");
        --backToSpeed = obj:GetChild("backToSpeed")
        --btnActive = obj:GetChild("btnActiveRes");
        --BtnBackActive = obj:GetChild("BtnActive");
        --btnAgree = obj:GetChild("btnAgree");
        --btnVoice = obj:GetChild("btnVoice");
        --SpeedPanel = obj:GetChild("SpeedPanel")
        --SpeedPanelAgreeText = obj:GetChild("AgreeNum");
        --for i = 1, 6 do
        --    local tempCardIcon = obj:GetChild("roleItem" .. i);
        --    table.insert(TabHighTeamIcon, tempCardIcon);
        --end
        --for i = 1, 3 do
        --    local singleFri = showBar:GetChild("friend" .. i);
        --    FriendSTab.visible = false;
        --    table.insert(FriendSTab, singleFri);
        --end
        if mapSize.x == nil then
            mapSize.x = mapContent.width;
        end
        if mapSize.y == nil then
            mapSize.y = mapContent.height;
        end

        -------------------------------------
        -- 资源掠夺
        
        -- this.n341 = obj:GetChild("plunder")
        -- print("--------------->", this.n341)
        Backbtn = obj:GetChild("Backbtn")
        -- this.btn_search = obj:GetChild("Btn_Seach")
        this.btn_refresh = obj:GetChild("Btn_Refresh") 
        this.btn_revenge_list = obj:GetChild("Btn_RevengeList")
        this.btn_defend_team = obj:GetChild("Btn_DefendTeam")
        -- this.btn_rob_list = obj:GetChild("Btn_RobList")

        this.btn_search_type_controller = obj:GetController("Type") 
        -- this.btn_search_controller = this.btn_search:GetController("button") 

        -- this.n344 = obj:GetChild("n344") 
        -- this.n345 = obj:GetChild("n345") 
        -- this.text_robnotice = obj:GetChild("Text_RobNotice")
        this.txt_max_tip = obj:GetChild("Text_RobMax")
        -- this.bg_progress = obj:GetChild("Bar_RobMax")
        this.txt_progress = obj:GetChild("Test_MaxNum")
        this.com_chest = obj:GetChild("Com_Chest")
        -- this.chest_controller = this.com_chest:GetController("chest")

        -- this.btn_add = this.n341:GetChild("Btn_Add")
        -- this.item_loader_icon = this.btn_search:GetChild("Loader_Cost")
        -- this.txt_lueduo_res = this.btn_search:GetChild("Text_CostNum")

        -- local item_cfg = dataTable.getRow("t_item", 200005)
        -- if item_cfg then 
        --     local item_cfg_icon = item_cfg.f_Icon 
        --     this.item_loader_icon.url = "ui://ItemAtlas/"..item_cfg_icon --
        -- end
        -- local plunder_count = logicMgr.ItemManager.getItemCountbyItemTypeAndItemModelId(EnumConst.ItemTableEnum.Item, 200005) or 0  
        --  -- local cost_item_str = dataTable.getDataTableByStringId("t_global", 332 ,"f_string_value")
        -- -- local cost_item_tbl = decodeJsonStrToTabel(cost_item_str) 
        -- -- local cost_item_id = tonumber(cost_item_tbl[1][1])
        -- -- local cost_item_num = cost_item_tbl and cost_item_tbl[1] and tonumber(cost_item_tbl[1][2]) or 0 
        -- -- if plunder_count >= cost_item_num then
        --     this.txt_lueduo_res.text = tostring(plunder_count)  --.. "/" .. cost_item_num
        -- -- else
        -- --      this.txt_lueduo_res.text = table.concat({string.TextColor(getNumString(plunder_count),"#FF0000"), "/", tostring(cost_item_num)})
        -- -- end

        this.Text_PowerPreview = obj:GetChild("Text_PowerPreview")
        this.btn_head = obj:GetChild("Btn_Head")
        local head = this.btn_head:GetChild("head")
        this.img_Icon = head:GetChild("HeadIcon")
        this.img_Icon.url = logicMgr.HeroManager.GetRoleIcon()
        this.img_frame = this.btn_head:GetChild("frame")
        local itemid = logicMgr.HeroManager.GetRoleInfo("picFrame")
        this.img_frame.url = EnumConst.AtlasPath.ICONAtlas .. itemid
        this.btn_help = obj:GetChild("Btn_Help")
        this.show_map_controller = obj:GetController("ShowMap")
        -- this.rob_open_controller = obj:GetController("RobOpen")

        this.btn_onoff = obj:GetChild("Btn_OnOff")
        this.btn_onoff_controller = this.btn_onoff:GetController("OnOff")
        this.txt_onoff_time = this.btn_onoff:GetChild("Text_Time")
        this.time_show_controller = this.btn_onoff:GetController("Time_Show")
        this.txt_onoff = obj:GetChild("Text_OnOff")
        -- this.txt_rob_count = obj:GetChild("Text_RobCount")
        -- this.txt_revenge = obj:GetChild("Text_Revenge")
        -- this.re_rob_controller = obj:GetController("ReRob")
        -------------------------------------

        this.addClick();
        --this.InitCountry(); 
    end
    if hasInit then
        --this.InitFriend();
        --pageController.selectedIndex = 0;
        --FriendList:SetActive(false);
        --ActivePanel:SetActive(false);
        --SpeedPanel:SetActive(false);
        --this.initMap();
        dungeonHandler.sendreqSpeedRace();
        this.closeDetail();

        -------------------------------------
        -- 资源掠夺    
        local cfg_up_rob_str = dataTable.getDataTableByStringId("t_global", 337 ,"f_string_value") 
        local power_compare_cfg = decodeJsonStrToTabel(cfg_up_rob_str)
        this.cur_power_compare_cfg = {}
        for k, v in ipairs(power_compare_cfg) do
            this.cur_power_compare_cfg[k] = {}
            this.cur_power_compare_cfg[k].start_value = tonumber(v[1])/10000
            this.cur_power_compare_cfg[k].end_value = tonumber(v[2])/10000
        end 

        this.cur_red_name_cfg = dataTable.getDataTableByStringId("t_global", 365 ,"f_string_value")  
        if plunderHandler.rob_total_time then
            local cur_total_time = plunderHandler.rob_total_time 
            local up_time = dataTable.getDataTableByStringId("t_global", 321 ,"f_int_value") 
            local cur_progress = cur_total_time/up_time
            if cur_progress > 1 then
                cur_progress = 1
            end
            cur_progress = math.ceil(cur_progress*100)
            -- this.bg_progress.value = cur_progress
            this.txt_progress.text = tostring(cur_progress).."%"
            if cur_progress<= 20 then  
                this.com_chest.url = "ui://UIBigMap/robbery_mn_icon01"
            elseif cur_progress <= 40 then 
                this.com_chest.url = "ui://UIBigMap/robbery_mn_icon02"
            elseif cur_progress <= 60 then 
                this.com_chest.url = "ui://UIBigMap/robbery_mn_icon03"
            elseif cur_progress <= 80 then 
                this.com_chest.url = "ui://UIBigMap/robbery_mn_icon04"
            else 
                this.com_chest.url = "ui://UIBigMap/robbery_mn_icon05"
            end
        end


        local open_chapter = dataTable.getDataTableByStringId("t_global", 330 ,"f_int_value")
        local cur_chapter = logicMgr.MapManager.GetMapcfgByID(MapManager.getMapId()).f_ChapterId or 1
        if open_chapter > cur_chapter or Config.OpenPlunder == false then 
            -- this.n341.visible = false 
            -- this.btn_search.visible = false 
            -- this.btn_refresh.visible = false 
            -- this.btn_revenge_list.visible = false 
            -- this.btn_defend_team.visible = false 
            -- -- this.btn_rob_list.visible = false
            -- -- this.n344.visible = false 
            -- -- this.n345.visible = false 
            -- -- this.text_robnotice.visible = false 
            -- this.txt_max_tip.visible = false 
            -- -- this.bg_progress.visible = false 
            -- this.txt_progress.visible = false 
            -- this.com_chest.visible = false
            -- this.btn_head.visible = false
            this.show_map_controller:SetSelectedIndex(0)
        else 
            if this.open_type then
                this.show_map_controller:SetSelectedIndex(0)
            else
                this.show_map_controller:SetSelectedIndex(1)
                -- -- this.n341.visible = true 
                -- this.btn_search.visible = true 
                -- this.btn_refresh.visible = true 
                -- this.btn_revenge_list.visible = true 
                -- this.btn_defend_team.visible = true 
                -- -- this.btn_rob_list.visible = true
                -- -- this.n344.visible = true 
                -- -- this.n345.visible = true 
                -- -- this.text_robnotice.visible = true 
                -- this.txt_max_tip.visible = true 
                -- -- this.bg_progress.visible = true 
                -- this.txt_progress.visible = true 
                -- this.com_chest.visible = true
                -- this.btn_head.visible = true
                this.btn_search_type_controller:SetSelectedIndex(1)  
                -- this.btn_search_controller:SetSelectedIndex(0) 
                plunderHandler.sendReqPlunderSearch()
            end
            this.ResUpdateOnOff()
        end
        this.Text_PowerPreview.text = HelperFunc.FormatResourceNum(logicMgr.HeroManager.GetHerosSumPower(), true);

        SetPlayerPrefs("uibigmap_click_world_map", "1") 
        -- SetPlayerPrefs("uibigmap_click_world_map_search", "1")
        FireEvent(Config.EventType.Fresh_WorldMap)
        -------------------------------------

    end

    --logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UIBigMap,Config.FuncOpenType.BattleRank, btnSpeed.name,btnSpeed);
end

function UIBigMap.OnEnable()
    this.LocalListenEvent();
    --UpdateBeat:Add(this.update, self)
    if Config.OpenPlunder == true then
        plunderHandler.sendReqPlunderInfo()   
    end
end

function UIBigMap.OnDisable()
    mapContent.scale = Vector2(1, 1);
    --mapContent:SetPosition(0, 0, 0);
    mapContent:SetSize(mapSize.x, mapSize.y);
    --UpdateBeat:Remove(this.update, self);
    --CloseEvent(Config.EventType.Fresh_Shop,this.refreshUI);
    logicMgr.FuncOpenManager.removeStatusCtr(PanelNames.UIBigMap);
end

function UIBigMap.LocalListenEvent()
    --ListenEvent(Config.EventType.Fresh_Shop,this.refreshUI);
end

function UIBigMap.OnDestroy() 
    hasInit = nil
    if this.time_id then
        utimer.remove(this.time_id)
        this.time_id = nil 
    end
    mapObjTab = {};
    this.removeClick()
 
    -- this.btn_search = nil
    this.btn_refresh = nil
    -- this.n341 = nil
    -- this.txt_lueduo_res = nil
    this.btn_search_type_controller = nil
    -- this.btn_search_controller = nil 
    this.btn_revenge_list = nil
    this.btn_defend_team = nil
    -- this.btn_rob_list = nil
    this.init_data = nil
end

--function UIBigMap.update()
--    if Input.GetAxis("Mouse ScrollWheel") < 0 then
--        local sx = Util.Lua_GetLocalScaleXYZ(mapContent.displayObject.gameObject, 0, 0, 0);
--        if (sx > 1) then
--            mapContent.scale = mapContent.scale + Vector2(-1 * MAP_SCALE, -1 * MAP_SCALE);
--            mapContent:SetSize(mapContent.scale.x + MAP_SCALE * -1, mapContent.scale.y + MAP_SCALE * -1);
--
--        end
--    end
--    if Input.GetAxis("Mouse ScrollWheel") > 0 then
--        local sx = Util.Lua_GetLocalScaleXYZ(mapContent.displayObject.gameObject, 0, 0, 0);
--        if (sx < 2) then
--            mapContent.scale = mapContent.scale + Vector2(MAP_SCALE, MAP_SCALE);
--            mapContent:SetSize(mapContent.scale.x + MAP_SCALE, mapContent.scale.y + MAP_SCALE);
--
--        end
--    end
--end

function UIBigMap.addClick()
    --LuaBehaviour:AddClick(DayBtnTab[i].gameObject,function() this.onClickDay(i); end);
    Backbtn.onClick:Add(this.onClickBack);
    btnOpenUnLockPage.onClick:Add(this.initUnLockPage);
    btnCloseUnLockPage.onClick:Add(this.onCloseUnLockPage);
    btnCloseMapDrop.onClick:Add(this.onCloseDropMapPage);
    btnbackMapDrop.onClick:Add(this.onCloseDropMapPage);
    this.Btn_ClickArea.onClick:Add(this.initMapStages);
    --showBar.onClick:Add(this.closeChapter);
    --closeBar.onClick:Add(this.closeChapter);
    --BtnFriback.onClick:Add(this.CloseFriend);
    --friendBg.onClick:Add(this.GotFriend);
    --btnSpeed.onClick:Add(this.reqSpeedRace);
    --BtnBackSpeedPanel.onClick:Add(this.CloseSpeedPanel);
    --backToSpeed.onClick:Add(this.BackToSpeed);
    --btnActive.onClick:Add(this.GotActiveData);
    --BtnBackActive.onClick:Add(this.BackToSpeed);
    --btnAgree.onClick:Add(this.Agreehigh);
    --btnVoice.onClick:Add(this.ShowVoice);
    --this.detailBtnGo.onClick:Add(this.goBattle);
    --this.detailBgClose.onClick:Add(this.closeDetail);
    --btnVoice.visible = false
    ListenEvent(Config.EventType.FreashDungeonStar, this.ShowSpeedPanel)
    --ListenEvent(Config.EventType.FreashDungeonAGree, this.GotDungeonStar)
    --ListenEvent(Config.EventType.FreashDungeonTask, this.ShowActive)
    --ListenEvent(Config.EventType.FreashDungeonTaskAward, this.GotDungeonAward)
    --ListenEvent(Config.EventType.FreashDungeonAGreeAlready, this.isAgreeAlready)
    --ListenEvent(Config.EventType.Res_TaskInfo_Get, this.initChapterTask);

    -------------------------------------------------------------
    -- 掠夺功能
    -- this.btn_search.onClick:Add(this.onClickSearch)
    this.btn_refresh.onClick:Add(this.onClickRefresh)
    this.btn_revenge_list.onClick:Add(this.onClickRevengeList)
    this.btn_defend_team.onClick:Add(this.onClickDefendTeam)
    this.com_chest.onClick:Add(this.onClickRobList)
    this.btn_help.onClick:Add(this.onClickHelp)
    this.btn_onoff.onClick:Add(this.onRobSwitch)
    -- this.btn_onoff.visible = false
    -- this.btn_rob_list.onClick:Add(this.onClickRobList)
    -- this.btn_add.onClick:Add(this.onClickAddPlunderItem)
    -------------------------------------------------------------

    -- UIRedDots.addViewListen(PanelNames.UIBigMap, "btn_search", this.btn_search, Config.RedDotsType.WorldMapSearch) -- 世界地图搜索红点
    UIRedDots.addViewListen(PanelNames.UIBigMap, "btn_revenge_list", this.btn_revenge_list, Config.RedDotsType.WorldMapRevengeList) -- 世界地图复仇列表红点
    UIRedDots.addViewListen(PanelNames.UIBigMap, "btn_defend_team", this.btn_defend_team, Config.RedDotsType.defFormation,EnumConst.FormationType.PlunderBattle) -- 世界地图防守阵容红点

    -- UIRedDots.addViewListen(PanelNames.UIBigMap, "btn_rob_list", this.btn_rob_list, Config.RedDotsType.WorldMapReceiveRobReward) -- 世界地图复仇列表红点
     
end

function UIBigMap.ResUpdateOnOff() 
    if this.time_id then
        utimer.remove(this.time_id)
        this.time_id = nil 
    end
    --print("ResUpdateOnOff------------------------>", plunderHandler.change_plunder_function_type, plunderHandler.last_change_func_time)
    if plunderHandler.change_plunder_function_type == 1 then
        
        -- this.btn_search_type_controller:SetSelectedIndex(1) 
        -- this.btn_search_controller:SetSelectedIndex(0)
        --print("plunderHandler.last_change_func_time:", plunderHandler.last_change_func_time)
        this.btn_onoff_controller:SetSelectedIndex(0)
        if plunderHandler.last_change_func_time == 0 then
            this.time_show_controller:SetSelectedIndex(1)
        else
            this.time_show_controller:SetSelectedIndex(0)
            local remain_time = dataTable.getDataTableByStringId("t_global", 363 ,"f_int_value")
            local dis_time = plunderHandler.last_change_func_time + remain_time  - Config.serverTime
            dis_time = math.ceil(dis_time/1000)
            if dis_time > 0 then
                this.time_show_controller:SetSelectedIndex(0)
                if this.txt_onoff_time then
                    local onoff_time_content
                    if dis_time > 60 then
                        local h, m, s = timeToTimeFormat2(dis_time)
                        onoff_time_content = string.format("%s:%s", h, m)
                    else 
                        onoff_time_content = string.format("%s", dis_time)
                    end 
                    this.txt_onoff_time.text = onoff_time_content
                    this.txt_onoff_time_content = onoff_time_content
                end
                
                this.time_id = utimer.timeCall(function()
                    dis_time = dis_time -1 
                    if this.txt_onoff_time then
                        local onoff_time_content
                        if dis_time > 60 then
                            local h, m, s = timeToTimeFormat2(dis_time)
                            onoff_time_content = string.format("%s:%s", h, m)
                        else 
                            onoff_time_content = string.format("%s", dis_time)
                        end
                        if this.txt_onoff_time_content == nil or this.txt_onoff_time_content ~= onoff_time_content then
                            this.txt_onoff_time.text = onoff_time_content
                            this.txt_onoff_time_content = onoff_time_content
                        end
                    end
                    if dis_time <= 0 then
                        if this.time_id then
                            utimer.remove(this.time_id)
                            this.time_id = nil 
                        end
                        this.time_show_controller:SetSelectedIndex(1)
                    end
                end, 1000, dis_time)
            else
                -- this.btn_onoff_controller:SetSelectedIndex(1)
                this.time_show_controller:SetSelectedIndex(1)
            end
        end
        -- this.rob_open_controller:SetSelectedIndex(0)
    else
       
        this.btn_onoff_controller:SetSelectedIndex(1)
        this.time_show_controller:SetSelectedIndex(1)
        -- local show_num = dataTable.getDataTableByStringId("t_global", 329 ,"f_int_value") or 1
        -- local cur_chapter = logicMgr.MapManager.GetMapcfgByID(MapManager.getMapId()).f_ChapterId or 1
        -- if cur_chapter < show_num then
        --     cur_chapter = show_num
        -- end 
        -- for i = cur_chapter, 1, -1 do   
        --     local item = chapterItems[i]
        --     if item then 
        --         local controller = item:GetController("head")  
        --         controller:SetSelectedIndex(0)  
        --     end
        -- end
        -- plunderHandler.have_search = nil 
        -- this.btn_search_type_controller:SetSelectedIndex(1) 
        -- this.rob_open_controller:SetSelectedIndex(1)
    end
    -- if this.btn_onoff.visible == false then
    --     this.btn_onoff.visible = true
    -- end
end

function UIBigMap.onRobSwitch()
    if plunderHandler.change_plunder_function_type == 1 then
        local remain_time = dataTable.getDataTableByStringId("t_global", 363 ,"f_int_value")
        local dis_time = plunderHandler.last_change_func_time + remain_time  - Config.serverTime 
        if dis_time > 0 then 
            UISysTips.AddMsg(getLanguage("ErrorCode_172083"))
        else
            showUI(PanelNames.UIOpenPopup, 1)
        end
    else 
        showUI(PanelNames.UIOpenPopup, 0)
    end
end

function UIBigMap.removeClick()
    
    UIRedDots.removeViewEvent(PanelNames.UIBigMap)

    Backbtn.onClick:Clear();
    btnOpenUnLockPage.onClick:Clear();
    btnCloseUnLockPage.onClick:Clear();
    btnCloseMapDrop.onClick:Clear();
    btnbackMapDrop.onClick:Clear();
    this.Btn_ClickArea.onClick:Clear();

    -- btn_closeChap.onClick:Clear();
    --BtnFriback.onClick:Clear();
    --friendBg.onClick:Clear();
    --closeBar.onClick:Clear();
    --btnSpeed.onClick:Clear();
    --BtnBackSpeedPanel.onClick:Clear();
    --backToSpeed.onClick:Clear();
    --BtnBackActive.onClick:Clear();
    --btnAgree.onClick:Clear();
    --this.detailBtnGo.onClick:Clear();
    --this.detailBgClose.onClick:Clear();
    CloseEvent(Config.EventType.FreashDungeonStar, this.ShowSpeedPanel)
    --CloseEvent(Config.EventType.FreashDungeonAGree, this.GotDungeonStar)
    --CloseEvent(Config.EventType.FreashDungeonTask, this.ShowActive)
    --CloseEvent(Config.EventType.FreashDungeonTaskAward, this.GotDungeonAward)
    --CloseEvent(Config.EventType.FreashDungeonAGreeAlready, this.isAgreeAlready)
    --CloseEvent(Config.EventType.Res_TaskInfo_Get, this.initChapterTask);

    -------------------------------------------------------------
    -- 掠夺功能
    -- this.btn_search.onClick:Clear()
    this.btn_refresh.onClick:Clear()
    this.btn_revenge_list.onClick:Clear()
    this.btn_defend_team.onClick:Clear()
    this.com_chest.onClick:Clear()
    this.btn_help.onClick:Clear()
    this.btn_onoff.onClick:Clear()
    -- this.btn_rob_list.onClick:Clear()
    -- this.btn_add.onClick:Clear()
    -------------------------------------------------------------

end

function UIBigMap.InitFriend()
    for i = 1, #FriendSTab do
        FriendSTab[i].visible = false;
    end
    local tab_myFriend = logicMgr.FriendManager.GetMyFriendNewList()
    table.sort(tab_myFriend, function(a, b)
        return a["dungeonId"] < b["dungeonId"]
    end)
    for i = 1, #tab_myFriend do
        if (i > 3) then
            break ;
        end
        --local tempF = FriendSTab[i]
        FriendSTab[i].visible = true;
        local temphero = logicMgr.HeroManager.getHeroDatabyModelId(tab_myFriend[i]["pic"], true);
        local IconS = FriendSTab[i]:GetChild("head"):GetChild("HeadIcon");
        local url = "ui://ICONAtlas/" .. temphero.icon;
        FriendSTab[i]:GetChild("name").text = temphero.name;
        IconS.url = url
        --resMgr:LoadIconFromPrefab("ICONAtlas", temphero.icon, function(sp)
        --    IconS.sprite = sp;
        --end);
    end
end

function UIBigMap.CloseFriend()
    --FriendList:SetActive(false);
    pageController.selectedIndex = 0;

end
function UIBigMap.BackToSpeed()
    pageController.selectedIndex = 1;

end

function UIBigMap.Agreehigh()
    dungeonHandler.sendReqDungeonStar();
end

function UIBigMap.reqSpeedRace()
    --if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.BattleRank, true) then
    dungeonHandler.sendreqSpeedRace();
    --dungeonHandler.sendReqDungeonStarInfo();
    --end
end

--- @param RaceData ResDungeonRankMessage
function UIBigMap.ShowSpeedPanel(RaceData)
    RaceData.myRank = RaceData.myRank + 1;
    this.initMap(RaceData)
end
--返回
function UIBigMap.onClickBack()
    if not isOpenUI(PanelResNames.UIHeadMenu) then
        showUI(PanelResNames.UIHeadMenu, true);
    end
    closeUI(PanelNames.UIBigMap);
end

function UIBigMap.initUnLockPage()
    unLockPageController:SetSelectedIndex(1);
    unLockDatas = MapManager.getMapUnLockCfg();
    unLockList.itemRenderer = this.rendererUnLockItem;
    unLockList.numItems = #unLockDatas;
end

function UIBigMap.rendererUnLockItem(idx, obj)
    local index = idx + 1;
    local data = unLockDatas[index]
    local nextData = unLockDatas[index + 1];
    local controller = obj:GetController("lock");
    local stageText = obj:GetChild("Text_UnlockStage");
    local unlockFuncText = obj:GetChild("Text_UnlockFunc");
    local stageStr = data.f_ChapterId .. "-" .. data.f_StageNum;
    stageText.text = stageStr;
    unlockFuncText.text = getLanguage(data.f_UnlockText)
    local mapId = logicMgr.MapManager.getMapId();
    if mapId == data.f_StageId-1  then
        --当前解锁
        controller:SetSelectedIndex(1)
    elseif mapId >= data.f_StageId then
        --已经解锁
        controller:SetSelectedIndex(0)
    else
        --未解锁
        controller:SetSelectedIndex(2)
    end

end

function UIBigMap.onCloseUnLockPage()
    unLockPageController:SetSelectedIndex(0);

end

function UIBigMap.onCloseDropMapPage()
    dropMapPageController:SetSelectedIndex(0);

end

function UIBigMap.rendererDropItem(idx, obj)
    local data = logicMgr.ItemManager.getItemConstDatabyModelId(dropListData[idx + 1]);
    local item = CtrlManager.GetCtrl(PanelNames.Item);
    item.initData(obj, data);
end

function UIBigMap.initMapDrop(data)
    dropMapPageController:SetSelectedIndex(1);

    local mapShowController = mapDropPage:GetChild("Com_SmallMap"):GetController("pos");
    mapShowController:SetSelectedIndex(data.chapterId)
    local chapter = mapDropPage:GetChild("Text_Chapter");
    chapter.text = data.chapter;
    local chapterName = mapDropPage:GetChild("Text_ChapterName");
    chapterName.text = data.name;
    local stageCfg = G.dataTable["t_stage"][data.chapterId .. "01"];

    local expResBar = mapDropPage:GetChild("Com_ResHeroExp"):GetChild("Text_ResNum");
    local goldResBar = mapDropPage:GetChild("Com_ResGold"):GetChild("Text_ResNum");
    local stoneResBar = mapDropPage:GetChild("Com_ResStone"):GetChild("Text_ResNum");
    expResBar.text = stageCfg.f_Exp;
    goldResBar.text = stageCfg.f_Coin;
    stoneResBar.text = math.floor(24 * 60 * stageCfg.f_EliteCred / 10000)

    dropListData = {}
    dropListData = decodeJsonStrToTabel(G.dataTable["t_chapter"][tostring(data.chapterId)].f_RewardShow);
    local dropList = mapDropPage:GetChild("List_Item");
    dropList.itemRenderer = this.rendererDropItem;
    dropList.numItems = #dropListData;
end

function UIBigMap.initMapStages()
    local cfg = logicMgr.MapManager.GetMapcfgByID(MapManager.getNextStage())
    local curChapter = 0
    if cfg then
        curChapter = cfg.f_ChapterId
    else
        curChapter = logicMgr.MapManager.GetMapcfgByID(MapManager.getMapId()).f_ChapterId;
    end
    local maxChapter = table.getn(G.dataTable["t_chapter"]);
    local endIndex = math.ceil(curChapter / 13) * 13;
    local startIndex = endIndex -12
    chapterData = {};
    while true do
        if endIndex >= maxChapter then
            endIndex = maxChapter;
        end
        if startIndex <= curChapter and curChapter <= endIndex then
            --当前13个
            break ;
        else
            startIndex = endIndex;
            endIndex = startIndex + 13;
        end
    end
    for i = startIndex, endIndex do
        local chapterInfo = G.dataTable["t_chapter"][tostring(i)];
        table.insert(chapterData, chapterInfo)
    end

    for i = 1, #chapterItems do
        local data = chapterData[i];
        local item = chapterItems[i];
        local controller = item:GetController("Type")
        if data ~= nil then
            item.visible = true;
            if data.f_Chapter == curChapter then
                controller:SetSelectedIndex(1)
                --utimer.timeCall(function()
                --    mapList.scrollPane:SetPosX((item.position.x - UIBigMap.mapList.width / 2) , true);
                --    mapList.scrollPane:SetPosY((item.position.y - UIBigMap.mapList.height / 2) , true);
                mapList.scrollPane:SetPercX((item.position.x)/ UIBigMap.mapContent.width, true);
                mapList.scrollPane:SetPercY((item.position.y)/ UIBigMap.mapContent.height, true);
                --end, 300)
                --local TweenSC = mapContent:TweenMove(Vector2(dotpos[1], dotpos[2]) * MAP_SCALE, 0.3) --
                --TweenSC:SetDelay(0.1);
            elseif data.f_Chapter < curChapter then
                controller:SetSelectedIndex(2)
            elseif data.f_Chapter > curChapter then
                controller:SetSelectedIndex(0)
            end

            local str = getLanguage(data.f_ChapterName);
            local chapterText = item:GetChild("Text_Chapter");
            local chapterNameText = item:GetChild("Text_ChapterName");
            local strLength = string.len(str)
            local param = {};
            param.chapterId = data.f_Chapter;
            for i = 1, strLength do
                if string.sub(str, i, i) == " " then
                    param.chapter = string.sub(str, 1, i)
                    chapterText.text = param.chapter;
                    param.name = string.sub(str, i, strLength)
                    chapterNameText.text = param.name;
                    break ;
                end
            end
            local btn_click_map_dot = item:GetChild("Btn_ClickMapDot")
            btn_click_map_dot.onClick:Clear()
            btn_click_map_dot.onClick:Add(function()
                if this.open_type then
                    this.initMapDrop(param)
                end
            end)

            local show_map_controller = item:GetController("ShowMap")
            local head_controller = item:GetController("head") 
            if this.open_type then
                show_map_controller:SetSelectedIndex(0)
                head_controller:SetSelectedIndex(0)
            else
                show_map_controller:SetSelectedIndex(1)
            end
            -- item.onClick:Clear()
            -- item.onClick:Add(function()
            --     this.initMapDrop(param)
            -- end)

        else
            item.visible = false;
        end
    end
 
    if this.init_data ~= nil and not this.open_type then
        --if plunderHandler.cur_search_list_data and next(plunderHandler.cur_search_list_data) ~= nil then
        --     this.ResPlunderSearch( plunderHandler.cur_search_list_data )
        --else
            this.ResPlunderSearch(plunderHandler.GetCurPlunderSearchData())
        --end
        
    end

end

function UIBigMap.initMap(RaceData)
    this.initMapStages();
    --showBar.visible = false;
    --closeBar.visible = false;
    ----chapterBg.gameObject:SetActive(false);
    ----friendBg.gameObject:SetActive(false);
    local nextStage = logicMgr.MapManager.GetMapcfgByID(logicMgr.MapManager.getMapcfg().f_NextStage)
    if (nextStage == nil) then
        nextStage = logicMgr.MapManager.getMapcfg()
    end
    --textNowChapinfo.text = getLanguage(nextStage.f_StageName);
    textNowSteep.text = nextStage.f_ChapterId .. "-" .. nextStage.f_StageNum;
    local tempRank = "--"
    if (RaceData.myRank > 0) then
        local sum = tonumber(tostring(RaceData.sum));
        tempRank =  math.floor((sum - RaceData.myRank) / sum * 100);
    end
    gameObject:GetChild("Text_OverPlayer").text = tempRank  .. "%";--我的排名
    --this.detailPoint.text = nextStage.f_ChapterId .. "-" .. nextStage.f_StageNum;
    ----mapContent.scale = mapContent.scale + Vector2(MAP_SCALE * distance, MAP_SCALE * distance);
    --mapContent:SetSize(mapSize.x * MAP_SCALE, mapSize.y * MAP_SCALE);
    --mapContent.scale = mapContent.scale * MAP_SCALE;
    --
    ----GetComponent("RectTransform"):
    ----mapName.text = logicMgr.MapManager.getMapName();
    ----local TweenSC = mapContent:TweenScale(Vector2(2, 2), 0.8) --GetComponent("RectTransform"):
    ----TweenSC:SetDelay(0.3);
    ----TweenSC:OnComplete(function()
    ----    mapContent:SetSize(2 * mapSize.x, 2 * mapSize.y);
    ----end)
    ----for i = 1, #mapObjTab do
    ----    destroy(mapObjTab[i]);
    ----end
    --lineTab = {};
    --this.chapterTaskBoxList = {};
    --local mapDeloyCfg = G.dataTable["t_mapdeploy"];
    --for k, v in pairsByKeys(mapDeloyCfg) do
    --
    --    local tempObj = mapObjTab[tonumber(k)];
    --    if tempObj == nil then
    --        tempObj = newGComponent("MapDot", "UIBigMap");
    --        mapObjTab[tonumber(k)] = tempObj;
    --    end
    --    mapContent:AddChild(tempObj);
    --    local dotpos = decodeJsonStrToTabel(v.f_Coordinates);
    --    --tempObj:SetPosition(mapContent.x + (0.5 - dotpos[1]) * tempObj.actualWidth, mapContent.y + (0.5 - dotpos[2]) * tempObj.actualHeight, mapContent.z);
    --    tempObj:SetPosition(dotpos[1] + mapSize.x / 2, dotpos[2] + mapSize.y / 2, 0);
    --    tempObj:SetScale(0.5, 0.5);
    --    --tempObj:GetComponent("RectTransform").anchoredPosition = Vector2(dotpos[1], dotpos[2]);
    --    local dotNode = decodeJsonStrToTabel(v.f_ChapterId);
    --    local allPoint = 0;
    --    local nowPoint = 0;
    --    for i = 1, #dotNode do
    --        local fiNode = dotNode[i][1];
    --        local lastNode = dotNode[i][2];
    --        local cutNode = lastNode - fiNode + 1;
    --        local tempMapid = nextStage.f_StageId;
    --        if (tempMapid >= fiNode and tempMapid <= lastNode) then
    --            nowPoint = allPoint + logicMgr.MapManager.getMapId() - fiNode;
    --            --utimer.timeCall(function()
    --            mapList.scrollPane:SetPosX((tempObj.position.x - UIBigMap.mapList.width /2) * MAP_SCALE, true);
    --            mapList.scrollPane:SetPosY((tempObj.position.y - UIBigMap.mapList.height /2) * MAP_SCALE, true);
    --            --mapList.scrollPane:SetPercY(1, true);
    --            --mapList.scrollPane:SetPercX(1, true);
    --
    --            --end, 500)
    --            --local TweenSC = mapContent:TweenMove(Vector2(dotpos[1] , dotpos[2])*MAP_SCALE, 0.3) --
    --            --TweenSC:SetDelay(0.1);
    --        end
    --        allPoint = allPoint + cutNode;
    --    end
    --    table.insert(lineTab, allPoint);
    --    table.insert(NowPointTab, nowPoint);
    --    local ficonfig = G.dataTable["t_stage"][tostring(dotNode[1][1])]
    --    local lastconfig = G.dataTable["t_stage"][tostring(dotNode[#dotNode][1])]
    --    local curConfig = G.dataTable["t_stage"][tostring(logicMgr.MapManager.getMapId())]
    --    local showController = tempObj:GetController("c1");
    --    local btnDot = tempObj:GetChild("btnDot");
    --    btnDot.onClick:Clear();
    --    btnDot.onClick:Add(function()
    --        this.showChapterDetail(ficonfig.f_ChapterId, nextStage.f_ChapterId);
    --    end);
    --    local data = {};
    --    data.box = tempObj:GetChild("btnTreasure");
    --    this.chapterTaskBoxList[ficonfig.f_ChapterId] = data;
    --    local previewController = tempObj:GetController("prview");
    --    previewController.selectedIndex = 0;
    --    showController.selectedIndex = 0;
    --    if nextStage.f_ChapterId < ficonfig.f_ChapterId then
    --        if ficonfig.f_ChapterId - nextStage.f_ChapterId <= G.dataTable["t_global"][tostring(193)].f_int_value then
    --            previewController.selectedIndex = 1;
    --        else
    --            previewController.selectedIndex = 2;
    --        end
    --    elseif ficonfig.f_ChapterId == nextStage.f_ChapterId then
    --        --当前关卡
    --        --tempObj:GetChild("mapName").text = getLanguage(v.f_Name);
    --    else
    --        showController.selectedIndex = 1;
    --    end
    --    tempObj:GetChild("mapName").text = ficonfig.f_ChapterId;
    --    tempObj:GetChild("charName").text = ficonfig.f_ChapterId .. "-" .. lastconfig.f_ChapterId .. getLanguage("CommonPop_Title20");
    --    --table.insert(mapObjTab, tempObj);
    --end
    --this.initChapterTask();
end

--function UIBigMap.showChapterDetail(chapter, nowChapter)
--    this.detailController.selectedIndex = 1;
--    this.detailTitle.text = getLanguage("MapChapter_" .. chapter);
--    if chapter == nowChapter then
--        this.detailStateController.selectedIndex = 1;
--    elseif chapter > nowChapter then
--        this.detailStateController.selectedIndex = 2;
--    else
--        this.detailStateController.selectedIndex = 0;
--    end
--    local taskConfig = logicMgr.TaskManager.getRemarkShowDataByType(EnumConst.AchieveTaskType.Chapter);
--    local taskTable;
--    for k, v in  pairs(taskConfig) do
--        if v.f_Progress == chapter then
--            taskTable = v;
--            break;
--        end
--    end
--    if taskTable ~= nil then
--        logicMgr.CostManager.RefreshItemList(this.detailRewardList, decodeJsonStrToTabel(taskTable.f_Award));
--        local serverData = logicMgr.TaskManager.getTaskServerData(taskTable.f_TaskId);
--        if serverData.status == -1 then
--            this.detailRewardController.selectedIndex = 3
--        elseif serverData.status == 1 then
--            this.detailRewardController.selectedIndex = 2
--            this.detailBtnReward.onClick:Clear();
--            this.detailBtnReward.onClick:Add(function ()
--                taskInfoHandler.SendReqTaskRewards(taskTable.f_TaskId);
--                this.closeDetail()
--            end);
--        else
--            this.detailRewardController.selectedIndex = 1
--        end
--    else
--        this.detailRewardController.selectedIndex = 0
--    end
--end

function UIBigMap.goBattle()
    this.onClickBack()
    showUI(PanelNames.UIBattleTeam, EnumConst.FightTypeConst.DUNGEON, nil, function(formations, _, Info)
        local curStage = logicMgr.MapManager.getNextStage();
        if curStage > 0 then
            dungeonHandler.sendBossMessage(curStage, formations[1], Info);
        end
    end, this.replyBattle);
end

function UIBigMap.closeDetail()
    --this.detailController.selectedIndex = 0;
end

--function UIBigMap.initChapterTask()
--    local taskConfig = logicMgr.TaskManager.getRemarkShowDataByType(EnumConst.AchieveTaskType.Chapter);
--    for k, v in pairs(taskConfig) do
--        local data = this.chapterTaskBoxList[v.f_Progress];
--        if data ~= nil then
--            local serverData = logicMgr.TaskManager.getTaskServerData(v.f_TaskId);
--            local taskBoxState = data.box:GetController("state");
--            data.box.onClick:Clear();
--            if serverData.status == -1 then
--                taskBoxState.selectedIndex = 2;
--            elseif serverData.status == 1 then
--                taskBoxState.selectedIndex = 1;
--                data.box.onClick:Add(function ()
--                    taskInfoHandler.SendReqTaskRewards(v.f_TaskId);
--                end);
--            else
--                taskBoxState.selectedIndex = 0;
--            end
--        end
--    end
--end

--function UIBigMap.clickMap(index)
--    showBar.visible = true;
--    closeBar.visible = true;
--    --chapterBg.gameObject:SetActive(true);
--    --friendBg.gameObject:SetActive(true);
--    --for i = 1, #LineObjTab do
--    --    destroyImmediate(LineObjTab[i]);
--    --end
--    LineObjTab = {}
--    local v = G.dataTable["t_mapdeploy"][tostring(index)]
--
--    dotNode = decodeJsonStrToTabel(v.f_ChapterId);
--    local list = showBar:GetChild("chapterContent");
--    list.itemRenderer = this.RenderListItem;
--    list.numItems = #dotNode;
--end
--
--function UIBigMap.RenderListItem(idx, obj)
--    local tempObj = obj:GetChild("chose");
--
--    local stateController = tempObj:GetController("c1");--0 fine 1 in 2 no
--    local fiNode = dotNode[idx + 1][1];
--    local lastNode = dotNode[idx + 1][2];
--    local nowPoint = 0;
--    local cutNode = lastNode - fiNode + 1;
--    if (logicMgr.MapManager.getMapId() <= lastNode and logicMgr.MapManager.getMapId() >= fiNode) then
--        nowPoint = logicMgr.MapManager.getMapId() - fiNode + 1;
--        stateController.selectedIndex = 1;
--
--    end
--    if (logicMgr.MapManager.getMapId() < fiNode) then
--        stateController.selectedIndex = 2;
--
--        nowPoint = 0;
--    end
--    if (logicMgr.MapManager.getMapId() > lastNode) then
--        stateController.selectedIndex = 0;
--        nowPoint = cutNode;
--    end
--    local ficonfig = G.dataTable["t_stage"][tostring(fiNode)]
--    --tempObj.obj:GetChild("cover").gameObject:SetActive(nowPoint UICharactorInfoHero== 0);
--    tempObj:GetChild("mapName").text = getLanguage(ficonfig.f_StageName);
--    tempObj:GetChild("charName").text = nowPoint .. "/" .. cutNode;
--    tempObj:GetChild("n254").onClick:Add(function()
--
--    end)
--    --table.insert(LineObjTab, tempObj);
--end

--function UIBigMap.updateTeamPlayer()
--    if BattleScene.isPlay() then
--        BattleScene.stop();
--        BattleScene.createPlayer();
--        BattleScene.start();
--    end
--end
--
--function UIBigMap.replyBattle()
--    if BattleScene.isPlay() then
--        BattleScene.stop();
--        BattleScene.createPlayer();
--        BattleScene.start();
--    else
--        BattleScene.createPlayer();
--        BattleScene.start();
--    end
--end

--function UIBigMap.closeChapter()
--    showBar.visible = false;
--    closeBar.visible = false;
--    --chapterBg.gameObject:SetActive(false);
--    --friendBg.gameObject:SetActive(false);
--end

--function UIBigMap.setData(itemobj, datasource)
--    local item = CtrlManager.GetCtrl(PanelNames.Item);
--    if item ~= nil then
--        item.initData(itemobj, datasource, LuaBehaviour);
--    end
--end

--------------------------------------------------------------
-- 资源掠夺

-- function UIBigMap.onClickSearch() 
--     if plunderHandler.change_plunder_function_type == 1 then 
--         FireEvent(Config.EventType.Fresh_WorldMapSearch)
--         plunderHandler.have_search = true
--         -- this.btn_search_controller:SetSelectedIndex(1)
--         if this.btn_search_controller.selectedIndex == 1 then
--         --     this.btn_search_controller:SetSelectedIndex(1)  
--             showUI(PanelNames.UISearching) 
--         end
--     end
-- end

-- {
-- ["order"] = 0,
-- ["targetList"] = {
-- },
-- ["state"] = 0,
-- ["msgId"] = 183102,
-- }
function UIBigMap.ResPlunderSearch( list_data ) 
    plunderHandler.cur_search_list_data = list_data or {}
    -- this.btn_search_controller:SetSelectedIndex(0) 
    -- this.btn_search_type_controller:SetSelectedIndex(1) 
    -- if list_data and next(list_data) ~= nil then
       
        this.UpdateRobPointData( list_data )
    -- else
    --     if this.btn_search_type_controller.selectedIndex == 0 then
    --         this.btn_search_type_controller:SetSelectedIndex(0)  
    --         this.btn_search_controller:SetSelectedIndex(0) 
    --     end
    -- end
    
end

function UIBigMap.UpdateRobPointData( list_data ) 
    if chapterItems and next(chapterItems) ~= nil then

        if list_data and next(list_data) ~= nil then
            table.sort(list_data, function( a, b )
                if a.power == b.power then
                    return a.lv > b.lv
                else
                    return a.power > b.power
                end 
            end)
        end

        local index = 1
        local cur_chapter = logicMgr.MapManager.GetMapcfgByID(MapManager.getMapId()).f_ChapterId or 1   
        cur_chapter = cur_chapter - math.floor(cur_chapter/13)*13 
        local param_b = dataTable.getDataTableByStringId("t_global", 335 ,"f_int_value")
        param_b = param_b/10000

        local show_num = dataTable.getDataTableByStringId("t_global", 329 ,"f_int_value") or 1
        if cur_chapter < show_num then
            cur_chapter = show_num
        end
        local rob_param1 = dataTable.getDataTableByStringId("t_global", 361 ,"f_string_value")
        local rob_param1_1 = tonumber(rob_param1[1])/10000
        local rob_param1_2 = tonumber(rob_param1[2])/10000

        local show_flag = nil
        for i = cur_chapter, 1, -1 do   
            local item = chapterItems[i]
            if item then
                
                local item_info = list_data[index]
                local controller = item:GetController("head") 
                if item_info then
                    if show_flag == nil then 
                        mapList.scrollPane:SetPercX((item.position.x)/ UIBigMap.mapContent.width, true);
                        mapList.scrollPane:SetPercY((item.position.y)/ UIBigMap.mapContent.height, true);
                        show_flag = true
                    end
                    controller:SetSelectedIndex(1)
                    local head = item:GetChild("head")
                    local btn_head = head:GetChild("Btn_Head")
                    local txt_power= head:GetChild("Text_PowerPreview")
                    local txt_name = head:GetChild("Text_playname")
                    local red_point = head:GetChild("red_point") 
                    local txt_time = head:GetChild("Text_Time")
                    local red_name_controller = head:GetController("RedName")
                    
                    if this.cur_red_name_cfg then
                        local num = #this.cur_red_name_cfg
                        local flag = true
                        for i = num, 1, -1 do
                            if item_info.red_val >= this.cur_red_name_cfg[i] then
                                red_name_controller:SetSelectedIndex(i)
                                flag = nil
                                break
                            end
                        end
                        if flag then
                            red_name_controller:SetSelectedIndex(0)
                        end
                    end

                    txt_time.text = getTimeStr(math.ceil(item_info.settle_time/1000))
                    txt_name.text = item_info.name or ""
                    -- txt_power.text = tostring(item_info.power)
                    item_info.playerId = tostring(item_info.playerId) 
                    this.showRobItemHeadInfo( btn_head, item_info, red_point, head) 
                    
                    local power_controller = head:GetController("Power") 
                    local total_power = item_info.total_power  or  0
                    txt_power.text = HelperFunc.FormatResourceNum(total_power)
                    local my_total_power = tonumber( tostring(logicMgr.HeroManager.GetHerosSumPower()) )
                    local value2 = total_power/my_total_power
                    value2 = math.pow(value2, param_b)
                    if value2 < rob_param1_1 then
                        value2 = rob_param1_1
                    elseif value2 > rob_param1_2 then
                        value2 = rob_param1_2-0.0000001
                    end
                    for k, v in ipairs(this.cur_power_compare_cfg) do
                       if value2 >= v.start_value and value2 < v.end_value then
                            power_controller:SetSelectedIndex(k-1)  
                            break
                       end 
                    end

                    if red_point ~= nil then
                        if plunderHandler.GetItemInfoFlagState( item_info.playerId ) then
                            red_point.visible = true
                        else
                            red_point.visible = false
                        end
                        -- if item_info.flag == true then
                        --     red_point.visible = true
                        -- else
                        --     red_point.visible = false
                        -- end
                    end 

                    local transition = head:GetTransition("Effect_Head")
                    transition:Play() 
                else
                    controller:SetSelectedIndex(0)
                end
                index = index + 1
            end
        end
    end
end

function UIBigMap.showRobItemHeadInfo( btn_head, item_info, red_point, head)

    btn_head:GetChild("head"):GetChild("HeadIcon").url = EnumConst.AtlasPath.ICONAtlas .."icon_hero_".. item_info.pic 
    btn_head:GetChild("frame").url = EnumConst.AtlasPath.ICONAtlas .. item_info.picFrame

    head.onClick:Clear() 
    head.onClick:Add(function()  
        if red_point then
            red_point.visible = false
        end
        plunderHandler.SetItemInfoFlagState( item_info.playerId, true )
        showUI(PanelNames.UIRob, item_info)
        -- if plunderHandler.cur_search_list_data then
        --     for k, v in pairs(plunderHandler.cur_search_list_data) do
        --         if item_info.playerId == v.playerId and plunderHandler.cur_search_list_data[k] then
        --             plunderHandler.cur_search_list_data[k].flag = false
        --         end
        --     end
        -- end
    end)
end

function UIBigMap.onClickRefresh() 
    plunderHandler.sendReqPlunderSearch() 
end

function UIBigMap.onClickRevengeList() 
    -- plunderHandler.SetRevengeListRedDotData( false )
    -- showUI(PanelNames.UIRevengeRecord) 
    showUI(PanelNames.UIRevengeRecord, 2)
end


function UIBigMap.onClickHelp() 
    local txts = {};
    txts["title"] = "RogueHelpTitle01"
    txts["des"] = "RogueHelpDes01" 
    showUI(PanelNames.CommonTips, txts);
end

function UIBigMap.onClickRobList()  
    showUI(PanelNames.UIRevengeRecord, 2)
end

function UIBigMap.onClickAddPlunderItem()  
     
    local need_num = dataTable.getDataTableByStringId("t_global", 323 ,"f_int_value") 
    local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop);
    pop.Init("48", function(b)
        if b then 
            local own_num = logicMgr.ItemManager.getCurrencyCountByModelId(CurrencyEnum.GOLD) or 0
            if own_num >= need_num then
                plunderHandler.sendReqBuyPlunderItem()
            else
                UISysTips.AddMsg(getLanguage("Character_Tips7"))
            end 
        end
    end, tostring(need_num), "1")

end

function UIBigMap.ResAddPlunderItem( msg )  
    -- if this.txt_lueduo_res then
    --     local plunder_count = logicMgr.ItemManager.getItemCountbyItemTypeAndItemModelId(EnumConst.ItemTableEnum.Item, 200005) or 0  
    --     this.txt_lueduo_res.text = tostring(plunder_count) 
    -- end
end

function UIBigMap.onClickDefendTeam()
    -- plunderHandler.sendReqPlunderInfo()  
    if not isOpenUI(PanelNames.UIBattleTeam) then
        local param = {}
        param.playerId = 0
        param.power = 0
        param.type = EnumConst.FormationType.PlunderBattle
        local def_formation = {}
        for i=1, 6 do
            def_formation[i] = plunderHandler.cur_def_formation[i]
        end
        param.formation = def_formation
        showUI(PanelNames.UIBattleTeam, EnumConst.FightTypeConst.Plunder, param, function(formations)
            local flag = true;
            if formations[1] ~= nil then
                for i = 1, #formations[1] do
                    if formations[1][i] ~= nil and tonumber(tostring(formations[1][i])) > 0 then
                        flag = false;
                        break ;
                    end
                end
            end
            if flag then
                UISysTips.AddMsg("需要上阵至少一名英雄");
            else  
                for i = 1, 6 do
                    if formations[1][i] == nil then
                        formations[1][i] = 0
                    end
                end
                plunderHandler.sendReqChangePlunderFormation( formations[1] )
                closeUI(PanelNames.UIBattleTeam)
                --if not isOpenUI(PanelResNames.UIHeadMenu) then
                --    showUI(PanelNames.UIHeadMenu)
                --end
            end 
        end, function() 
        end)
    end
end

-- function UIBigMap.onClickRobList()
--     -- showUI(PanelNames.UIRobRecord) 
--     showUI(PanelNames.UIRevengeRecord, 1)
-- end

function UIBigMap.ResPlunderInfo( msg )
    -- if msg and msg.defFormation  then
    --     this.cur_def_formation = msg.defFormation 
    --     for k, v in ipairs(this.cur_def_formation) do
    --         this.cur_def_formation[k] = tostring(v)
    --     end
    -- end

    if msg and msg.totalPlunderTime then
        local cur_total_time = tonumber( tostring(msg.totalPlunderTime) )  
        local up_time = dataTable.getDataTableByStringId("t_global", 321 ,"f_int_value") 
        local cur_progress = cur_total_time/up_time
        if cur_progress > 1 then
            cur_progress = 1
        end
        cur_progress = math.ceil(cur_progress*100)
        -- this.bg_progress.value = cur_progress
        this.txt_progress.text = tostring(cur_progress).."%"
        if cur_progress<= 20 then  
            this.com_chest.url = "ui://UIBigMap/robbery_mn_icon01"
        elseif cur_progress <= 40 then 
            this.com_chest.url = "ui://UIBigMap/robbery_mn_icon02"
        elseif cur_progress <= 60 then 
            this.com_chest.url = "ui://UIBigMap/robbery_mn_icon03"
        elseif cur_progress <= 80 then 
            this.com_chest.url = "ui://UIBigMap/robbery_mn_icon04"
        else 
            this.com_chest.url = "ui://UIBigMap/robbery_mn_icon05"
        end
    end

    print("我的红名值：", msg and msg.redVal)
    -- if msg and msg.redVal then
    -- end

    -- if msg and msg.totalPlunderVal then
    --     this.txt_rob_count.text = "累计突袭成功".. msg.totalPlunderVal .. "次"
    --     this.txt_rob_count.visible = true
    --     -- this.txt_revenge.text = ""
    --     this.re_rob_controller:SetSelectedIndex(0)
    -- end
end
--------------------------------------------------------------

return UIBigMap

